package cn.rkylin.oms.common.jobs.shop;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cn.rkylin.apollo.common.util.HttpUtils;
import cn.rkylin.oms.common.jobs.AbstractJob;
import cn.rkylin.oms.system.shop.domain.Shop;

/**
 * 退款单下载
 * /refundManager/getRefundAll.action
 *
 * @author wangxiaoyi
 */
@Component("returnOrderDownloadJob")
public class ReturnOrderDownloadJob extends AbstractJob {

    @Value("${executor.return.order.download.url}")
    private String returnOrderDownloadURL;

    @Value("${gateway}")
    private String gateway;

    /**
     * 退款单下载
     */
    @Override
    public <T> void createJob(T entity) throws Exception {
        Shop shop = (Shop) entity;
        // 没有配置店铺作业开关，或者开关==false则退出
        if (StringUtils.isEmpty(createShopJobs) || createShopJobs.equalsIgnoreCase("false")) {
            return;
        }
        postDatas.put("jobDetail.group", shop.getPrjId());
        postDatas.put("jobDetail.name", "shop_" + shop.getShopId() + "_ReturnOrderDownload");
        postDatas.put("className", "com.taobao.ad.easyschedule.job.HttpJob");
        postDatas.put("jobDetail.description", "平台退款单下载_" + shop.getShopName());
        postDatas.put("cloneTime", "");
        postDatas.put("triggerType", "Cron");
        postDatas.put("startTimeAsDate", "");
        postDatas.put("stopTimeAsDate", "");
        postDatas.put("cronExpression", "45 0/1 * * * ?");
        postDatas.put("repeatCount", "4");
        postDatas.put("repeatInterval", "16000");
        postDatas.put("priority", "4");
        postDatas.put("action", "1");
        postDatas.put("projectId", shop.getPrjId());
        postDatas.put("projectName", shop.getPrjName());

        // 重复名值对
        repeatNames.add("targetUrl");
        if ("小V铺".equalsIgnoreCase(shop.getShopType())) {
            repeatValues.add(gateway + "/xvp-microservice/refund/downloadXvpRefund");
        } else if ("唯品会直发".equalsIgnoreCase(shop.getShopType())) {
            repeatValues.add(gateway + "/microservice-vip/refund/downloadVipRefund");
        } else if ("淘宝分销".equalsIgnoreCase(shop.getShopType())) {
            repeatValues.add(gateway + "/microservice-fenxiao/refund/downloadFenXiaoRefundByUpdateTime");
        }else if ("网易考拉".equalsIgnoreCase(shop.getShopType())) {
            repeatValues.add(gateway + "/microservice-kaola/refund/downloadKaolaRefund");
        } else if("苏宁".equalsIgnoreCase(shop.getShopType())){
            repeatValues.add(gateway + "/microservice-suning/refund/downloadSuningRefund");
        } else if("商城".equalsIgnoreCase(shop.getShopType())){
            repeatValues.add(gateway + "/microservice-yiyouyayin/refund/downloadYiRefund");
        } else {
            repeatValues.add(returnOrderDownloadURL);
        }

        repeatNames.add("synchronous");
        repeatValues.add("true");

        repeatNames.add("parameter");
        repeatValues.add("");

        repeatNames.add("tokenValidate");
        repeatValues.add("false");

        repeatNames.add("checkResult");
        repeatValues.add("true");

        repeatNames.add("runType");
        repeatValues.add("hash");

        repeatNames.add("completeTime");
        repeatValues.add("60000");

        repeatNames.add("connTimeout");
        repeatValues.add("60000");

        repeatNames.add("retries");
        repeatValues.add("0");

        repeatNames.add("repeatAlarmNum");
        repeatValues.add("0");

        try {
            String addJobResult = HttpUtils.postMethod(addJobURL, postDatas, repeatNames, repeatValues);
            if (addJobResult.equals("error")) {
                throw new Exception("店铺：" + shop.getShopName() + "创建“退款单下载”作业失败。");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new Exception("店铺：" + shop.getShopName() + "创建“退款单下载”作业出现异常。");
        }
    }

}
